/*
* Copyright 2011 SmartDocuments. All rights reserved.
* SMARTDOCUMENTS PROPRIETARY/CONFIDENTIAL.
*
*    creation date: Mar 14, 2011
*    version: 1.0
*/
package org.opu.pc.security;

import org.opu.pc.domain.UserManagement;
import org.opu.pc.domain.dao.UserDao;
import org.opu.pc.domain.domains.security.User;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class UserDetailsServiceImpl implements UserDetailsService {
	
	private UserManagement userManagement;

    public UserDetailsServiceImpl(UserManagement userManagement) {
        this.userManagement = userManagement;
    }
    
    @Override
    public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {

		if (username == null || username.isEmpty()) {
			throw new UsernameNotFoundException("username not specified");
		}
		
		User user = userManagement.getNotGuestUserByName(username);
		
		if (user == null) {
			throw new UsernameNotFoundException("username '" + username + "' not found");
		}
		
		return new UserDetailsData(user);
	}

}
